import QuitDialog from '../QuitDialog/QuitDialog.vue';
import AddDialog from '../AddDialog/AddDialog.vue';
import RebootDialog from '../RebootDialog/RebootDialog.vue';

export default {
    components: {
        AddDialog,
        QuitDialog,
        RebootDialog
    },
    name: 'AccountManage',
    data() {
        return {
            //
            loading: true,
            // 当前页
            currentPage: 1,
            // 每页显示多少条
            pageSize: 1,
            // 总页数
            total: 1,
            // 展开收起搜索条件
            expandConditionFlag: false,
            // 现场勘察
            sceneProspectFlag: false,
            // 启用
            rebootFlag: false,
            // 停用
            quitOrderFlag: false,
            // 工单编号
            workOrderNo: '',
            // 手机号
            phoneNumber: '',
            // 状态
            workOrderStatus: '',
            accountId: '',
            orderStatusList: [
                {
                    value: '0',
                    label: '无效'
                },
                {
                    value: '1',
                    label: '有效'
                }
            ],
            // 表格数据
            tableData: [],
            multipleSelection: []
        };
    },
    created() {
        // 获取表格数据  current = 1, size = 10
        this.orderTableData(1, 10);
    },
    methods: {
        // 每页显示多少条, 重置current从1开始
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.pageSize = val;
            this.currentPage = 1;
            this.orderTableData(this.currentPage, val);
        },
        // 当前页数
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.orderTableData(val, 10);
        },
        status(status) {
            let result = this.orderStatusList.filter(item => item.value === status)[0];
            return result.label;
        },
        // 处理后端数据
        formatResult(list) {
            // 处理后端数据格式
            const resultFormat = list.map(item => {
                return {
                    // 状态
                    accountStatus: item.accountStatus,
                    // 供应商编号
                    supplierCode: item.supplierCode,
                    // 供应商名称
                    supplierName: item.supplierName,
                    // 手机号
                    userPhone: item.userPhone,
                    // 姓名
                    userName: item.userName,
                    // 创建人
                    createEmp: item.createEmp,
                    // 创建时间
                    createTm: item.createTm,
                    // 修改人
                    updateEmp: item.updateEmp,
                    // 修改时间
                    updateTm: item.updateTm,
                    //
                    accountId: item.accountId
                };
            });
            return resultFormat;
        },
        // 获取表格数据
        orderTableData(current, size) {
            this.$httpExt()
                .post('/api/pmsSupplierAccount/page', {
                    condition: {
                        supplierCode: this.supplierCode,
                        accountStatus: this.workOrderStatus,
                        userPhone: this.phoneNumber
                    },
                    current: current,
                    size: size
                })
                .then(
                    response => {
                        console.log('返回值', response.data);
                        let format = this.formatResult(response.data.records);
                        this.loading = false;
                        this.total = response.data.total;
                        this.tableData = format;
                    },
                    response => {
                        Notification.error({
                            title: '异常',
                            message: response.msg
                        });
                    }
                );
        },
        // 查询
        async resultQuery() {
            this.loading = true;
            this.orderTableData(1, 10);
        },
        // 重置搜索条件
        resetQuery() {
            this.workOrderStatus = '';
            this.workOrderNo = '';
            this.phoneNumber = '';
            this.orderTableData(1, this.pageSize);
        },
        // 选中行
        handleSelectionChange(level) {
            this.multipleSelection = level;
        },
        expandCondition() {
            this.expandConditionFlag = !this.expandConditionFlag;
        },
        // 新增弹框
        sceneProspect() {
            this.sceneProspectFlag = true;
        },
        // 关闭
        closeDialog() {
            this.sceneProspectFlag = false;
            this.quitOrderFlag = false;
            this.rebootFlag = false;
            // 刷新列表数据
            this.orderTableData(1, 10);
        },
        // 启用框打开
        reboot() {
            // 如果用户未选中，则提示先选择一条数据
            if (this.multipleSelection.length !== 1) {
                this.$message({
                    type: 'warning',
                    message: '请选择一条数据'
                });
                return false;
            }
            this.rebootFlag = true;
            this.accountId = this.multipleSelection[0].accountId;
        },
        // 停用框打开
        quitOrder() {
            // 如果用户未选中，则提示先选择一条数据
            if (this.multipleSelection.length !== 1) {
                this.$message({
                    type: 'warning',
                    message: '请选择一条数据'
                });
                return false;
            }
            this.quitOrderFlag = true;
            this.accountId = this.multipleSelection[0].accountId;
        }
    }
};
